//
//  AllproductsViewController.swift
//  liaoningdangdai
//
//  Created by 白雨桐 on 2019/11/26.
//  Copyright © 2019 白雨桐. All rights reserved.
//

import UIKit
import SwiftyJSON
import YYModel

//全部商品
class AllproductsViewController: BaseViewController, UITableViewDelegate,UITableViewDataSource {
    
    
    
    
    
    
    
    // 绑定标签
    @IBOutlet weak var allTable: UITableView!
    
    // 定义数组  Use of undeclared type 'Status'  数组 : [模型Model]
    private lazy var ViewModels : [AllProducts] = [AllProducts]()
    
    var newsArray : NSArray = []
    
    override func viewDidLoad()
    {
        super.viewDidLoad()

        if !isLogin {
            return
        }
        strInfo()
    }
    

}


extension AllproductsViewController
{
    
}


// MARK: - 获取数据   Allproducts
extension AllproductsViewController
{
    
    // 当前页面校验登陆，如果未登陆，滚到登陆页登陆
    @objc func strInfo()
    {
        let user = User()
        user.U = self.readWithFile(a: 1) as? String
        user.RETCODE = self.readWithFile(a: 2) as? String
        if ( (user.U == nil || user.U == "" ) || (user.RETCODE == nil || user.RETCODE == "") )
        {
            print("请登陆")
            self.goBack(identifier: "Login")
        }
        // 用户验证
        self.loadcheck_user(user: user)
        // 调用行情接口
        self.loadcommodity_data_query(user: user)
    }
    
    
    // 接口，用户验证
    @objc private func loadcheck_user(user : User) {
        // 用户验证
        let str1 = [user.U!,"101",user.RETCODE! ]
        NetworkTools.share.check_user(code: str1 as Array<Any>) { (result, error) in
            
            if error != nil {
                print("no no no")
                return
            }
            
            // 请登陆
            if self.pLogin(result: result!)  == false {
                return
            }
        }
    }
    
    // 获取当前页的接口数据： 行情接口
    @objc private func loadcommodity_data_query(user : User)
    {
        // 行情
        let str = [user.U!,user.RETCODE! ,""]
        NetworkTools.share.commodity_data_query(code: str as Array<Any>) { (result, error) in
            
            // 错误校验
            if error != nil {
                print("no no no")
                return
            }
            // print(result!)
            
            // 获取可选类型中的数组
            guard let resultArray = result else {
                print("no")
                return
            }
            
            // 请登陆
            if self.pLogin(result: result!)  == false {
                return
            }
            // 遍历数组转字典
            //let tempArray = resultArray["REP"]?["RESULTLIST"] as! [String : AnyObject];
            // 调用模型
            
            
            for tempDict in resultArray["REP"]?["RESULTLIST"] as! [String : AnyObject]{
                //for rep in tempDict.value as! [String : AnyObject] {
                    //for rec in rep.value as! [String : AnyObject] {
                        self.newsArray = tempDict.value as! NSArray
                
                    //}
                    
                //}
            }
            
            for cs in self.newsArray {
                let sta = AllProducts(dict: cs as! [String : AnyObject])
                self.ViewModels.append(sta)
                //let viewModel = AllProductsViewModel(AllProducts: sta)
                //self.ViewModels.append(viewModel)
                
            }
            /*
            for tempDict in resultArray {
                let sta = AllProducts(dict: tempDict.value as! [String : AnyObject])
                let viewModel = AllProductsViewModel(AllProducts: sta)
                self.ViewModels.append(viewModel)
            }
            */
            
            // 刷新表格
            self.allTable.reloadData()
            
        }
    }
    
}


// MARK: - TableView的数据源方法
extension AllproductsViewController
{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.ViewModels.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
         // 创建cell  AllmerchandiseCellId
        let cell : AllProductsViewCell = tableView.dequeueReusableCell(withIdentifier: "AllmerchandiseCellId") as! AllProductsViewCell
               
               // 给cell设置数据
               //let ViewModel = ViewModels[indexPath.row]
               //cell.textLabel?.text = ViewModel.CON
               //ell = ViewModel[indexPath.row]
               //cell.ViewModel = ViewModels[indexPath.row]
               //cell.CON = self.ViewModel.CON
               //cell.CON as uil = self.cs.CON
        cell.ViewModel = ViewModels[indexPath.row]
        //cell.CON = statuse.isCON
                
        /*
        cell.CON.text = statuse.CON
        
        cell.purLowBP.text = statuse.BP
        cell.purHighBP.text = "bb"
        cell.purLowBQ.text = "cc"
        cell.purQ.text = "1"
        
        cell.sellLowBP.text = "AA"
        cell.sellHighBP.text = "BB"
        cell.sellLowBQ.text = "CC"
        cell.sellQ.text = "22"
        */
               return cell
    }
    
    // cell高
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 260
    }
}
